
package mwt.xml.xdbforms.dblayer.impl.datasource;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import mwt.xml.xdbforms.dblayer.ConnectionFactory;
import mwt.xml.xdbforms.dblayer.metadata.CatalogMetaData;
import mwt.xml.xdbforms.dblayer.metadata.TableMetaData;
import mwt.xml.xdbforms.dblayer.metadata.impl.JDBCTableMetaDataDS;
import mwt.xml.xdbforms.dblayer.query.Query;
import mwt.xml.xdbforms.dblayer.query.impl.datasource.JDBCQuery;

/**
 * Implementazione di una ConnectionFactory che fa uso del
 * DataSource
 * @author Gianfranco Murador, Matteo Ferri, Cristian Castiglia
 */
public class ConnectionFactoryImpl extends ConnectionFactory {

    private DataSource dataSource;
    private static Properties properties;
    /**
     * Carico il driver ed il file di proprietà in questo punto
     **/


    static {

        try {
            Properties prop = new Properties();
            
            @SuppressWarnings("static-access")
            URL url = Thread.currentThread().getContextClassLoader().getResource("mwt/xml/xdbforms/configuration/xdbforms.properties");
            InputStream inStream = url.openStream();
            prop.load(inStream);
            ConnectionFactoryImpl.properties = prop;



        } catch (IOException ex) {
            System.err.println("Error: file properties not detected");
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public ConnectionFactoryImpl(DataSource dataSource) {
        super();
        this.dataSource = dataSource;
    }

    @Override
    public Query getQuery() {
        return new JDBCQuery(dataSource);
    }

    @Override
    public CatalogMetaData getDatabaseMetaData() {

        try {
            String className = ConnectionFactoryImpl.properties.getProperty("jdbc.datasource.catalogmdimpl");
            Class classDef = Class.forName(className);
            Class[] args = new Class[]{DataSource.class};
            Object[] vargs = new Object[]{dataSource};
            Constructor c = classDef.getConstructor(args);
            CatalogMetaData cmd = (CatalogMetaData) c.newInstance(vargs);

            // return new MySQLCatalogMetaDataDS(dataSource);
            return cmd;

        } catch (InstantiationException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectionFactoryImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;

    }

    @Override
    public TableMetaData getTableMetaData(String table) {

        return new JDBCTableMetaDataDS(table, dataSource);

    }
}
